 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NodeManager;

import java.io.File;

/**
 *
 * @author Conor
 */
public class NodeManagerService extends Thread {

    /* This class simply runs a thread to count to 50 */
    private boolean keepChecking = true;
    private RdfModelManager _rdfModelManager;
    private File _rdfFile = new File(Global.Data.RdfFile);
    IRdfViewer _mainGui;

    public NodeManagerService(RdfModelManager _rdfModelManager, IRdfViewer mainGui) {
        this._rdfModelManager = _rdfModelManager;
        _mainGui = mainGui;
    }

    @Override
    public void run() {
        while (keepChecking) {
            try {
                boolean refreshTable = false;
                System.out.println("checking : ");

                // If a node is unavailable, we will put this on hold until it has been repaired.
                if (_rdfModelManager.GetNode1().NodeAvailable() &&
                        _rdfModelManager.GetNode2().NodeAvailable() &&
                        _rdfModelManager.GetNode3().NodeAvailable()) {
                    refreshTable = _rdfModelManager.VerifyNodes();  // Ensure that each file has at least 2 nodes.
                    refreshTable = _rdfModelManager.checkForDamagedFiles();
                    refreshTable = _rdfModelManager.checkForAddedFiles();
                    refreshTable = _rdfModelManager.checkForModifiedFiles();
                    if (refreshTable) {
                        _mainGui.Refresh();
                    }
                }

                String message = "";
                if (_rdfModelManager.GetNode1().NodeAvailable()) {
                    message += " " + _rdfModelManager.GetNode1().NodeName;
                }
                if (_rdfModelManager.GetNode2().NodeAvailable()) {
                    message += " " + _rdfModelManager.GetNode2().NodeName;
                }
                if (_rdfModelManager.GetNode3().NodeAvailable()) {
                    message += " " + _rdfModelManager.GetNode3().NodeName;
                }
                _mainGui.Message(message);

                sleep(5000);
            } catch (InterruptedException e) {
            }

        }
    }

    public void stopService() {
        this.keepChecking = false;
    }

    public void StartService() {
        this.keepChecking = true;
    }
}

